﻿namespace TfsExtensions.Build.Helper
{
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>
    /// Right to left aware message box
    /// </summary>
    public static class RightToLeftAwareMessageBox
    {
        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The Message Box owner.</param>
        /// <param name="text">The Message Box text.</param>
        /// <param name="caption">The Message Box caption.</param>
        /// <param name="buttons">The Message Box buttons.</param>
        /// <param name="icon">The Message Box icon.</param>
        /// <param name="defaultButton">The default button.</param>
        /// <param name="options">The options.</param>
        /// <returns>Dialog Result</returns>
        public static DialogResult Show(
                                        IWin32Window owner, 
                                        string text, 
                                        string caption, 
                                        MessageBoxButtons buttons,
                                        MessageBoxIcon icon, 
                                        MessageBoxDefaultButton defaultButton,
                                        MessageBoxOptions options)
        {
            if (IsRightToLeft(owner))
            {
                options |= MessageBoxOptions.RtlReading |
                           MessageBoxOptions.RightAlign;
            }

            return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
        }

        /// <summary>
        /// Determines whether [is right to left] [the specified owner].
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <returns>
        /// <c>true</c> if [is right to left] [the specified owner]; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsRightToLeft(IWin32Window owner)
        {
            var control = owner as Control;

            if (control != null)
            {
                return control.RightToLeft == RightToLeft.Yes;
            }

            // If no parent control is available, ask the CurrentUICulture
            // if we are running under right-to-left.
            return CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
        }
    }
}